package com.program.facesigninsystem.controller;

import com.program.facesigninsystem.entity.ResponseModel;
import com.program.facesigninsystem.entity.UserInfo;
import com.program.facesigninsystem.service.IUserInfoService;
import com.program.facesigninsystem.util.HttpClient;
import com.program.facesigninsystem.util.TokenUtil;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Random;

@CrossOrigin
@RestController
@RequestMapping(value = "/auth")
public class UserController {
    final private String APP_ID = "wx8b04000a8e310474";
    final private String SECRET = "e79a1ce78b98e5addd495f3e6970920e";

    @Autowired
    private IUserInfoService iUserInfoService;
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    private ResponseModel loginUser(@RequestBody JSONObject account){
        ResponseModel responseModel;
        String openid = "";
        try{
            openid = HttpClient.getWxUserOpenid(account.get("code").toString(),APP_ID,SECRET).get("openid").toString();
        }catch (Exception e){
            responseModel = new ResponseModel(199,"微信授权失败");
            return responseModel;
        }
        UserInfo userInfo = iUserInfoService.loginUser(openid);
        if(userInfo!=null) {
            //判断该用户token是否为空
            if(userInfo.getToken()==null||userInfo.getToken().equals("")){
                String token = TokenUtil.createToken(userInfo.getUid(),userInfo.getOpenid(),1000*60l);
                userInfo.setToken(token);
                iUserInfoService.updateToken(userInfo.getUid(),token);//token存入用户数据库
            }
            else{
                if(!TokenUtil.decodeToken(userInfo.token)){
                    String token = TokenUtil.createToken(userInfo.getUid(),userInfo.getOpenid());
                    userInfo.setToken(token);
                    iUserInfoService.updateToken(userInfo.getUid(),token);//token存入用户数据库
                }
            }
            responseModel = new ResponseModel(200,"登录成功",userInfo);
        }
        else{
            String uid = "";
            Random random = new Random();
            for (int i = 0; i < 20; i++) {
                uid += String.valueOf(random.nextInt(10));
            }
            userInfo = new UserInfo();
            String token = TokenUtil.createToken(uid,openid);
            userInfo.setToken(token);
            userInfo.setUid(uid);
            userInfo.setOpenid(openid);
            iUserInfoService.addUser(uid,openid,token);
            responseModel = new ResponseModel(200,"登录成功",userInfo);
        }
        return responseModel;
    }

    @RequestMapping(value = "/updateUserById",method = RequestMethod.POST)
    private ResponseModel updateUserById(@RequestBody JSONObject data){
        String uid=data.get("uid").toString();
        String name;
        int type;
        String major;
        String uclass;
        String university;
        String academy;
        try{ name=data.get("name").toString(); }
        catch(Exception e){ name=null; }

        //当无更新参数type，type默认值100
        try{ type=data.getInt("type"); }
        catch (Exception e){ type=100; }

        try{ major=data.get("major").toString(); }
        catch (Exception e){ major=null; }

        try { uclass=data.get("uclass").toString(); }
        catch (Exception e){ uclass=null; }

        try{ university=data.get("university").toString(); }
        catch (Exception e){ university=null; }

        try{ academy=data.get("academy").toString(); }
        catch (Exception e){ academy=null; }

        //更新用户信息
        ResponseModel responseModel;
        if(name==null&&type==100&&major==null&&uclass==null&&university==null&&academy==null){
            responseModel = new ResponseModel(200,"无必要数据传回");
        }else{
            try{
                iUserInfoService.updateUser(uid,name,type,major,uclass,university,academy);
                responseModel = new ResponseModel(200,"更新成功");
            }catch (Exception e) {
                responseModel = new ResponseModel(199, "更新失败");
            }
        }
        return responseModel;
    }

    @RequestMapping(value = "/selectUserById",method = RequestMethod.POST)
    private ResponseModel selectUserById(@RequestBody JSONObject data){
        String uid=data.get("uid").toString();
        //返回用户信息
        UserInfo userInfo=iUserInfoService.selectUser(uid);
        ResponseModel responseModel;
        if(userInfo!=null){
            if(userInfo.getName()==null){ userInfo.setName(""); }
            if(userInfo.getMajor()==null){ userInfo.setMajor(""); }
            if(userInfo.Class==null){ userInfo.setClass(""); }
            if(userInfo.getUniversity()==null){ userInfo.setUniversity(""); }
            if(userInfo.getAcademy()==null){ userInfo.setAcademy(""); }
            responseModel = new ResponseModel(200,userInfo);
        }else{
            responseModel = new ResponseModel(199,"该用户不存在");
        }
        return responseModel;
    }
}
